<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="../common/TreeListUIHead.jsp"></jsp:include>
<title>组织</title>
</head>
<body style="padding:1px 3px 1px 3px;overflow:hidden">
	<div id="main">
		<div position="left">
			<ul id="leftTree" class="ztree"></ul>
		</div>
		<div position="center">
			<div id="toolBar">
			</div>
			<div id="tableContainer">
			</div>
			<c:if test="${multi == 'true'}">
				<div id="toolBar2"></div>
				<div id="selectTable"></div>
			</c:if>
	    </div>
    </div>
</body>
</html>
<script type="text/javascript">
	$tree_container = "leftTree";
	$tree_async_url = getPath()+"/permission/permissionGroup/treeData";
	var multi = false;
	<c:if test="${multi == 'true'}">
		multi = true;
	</c:if>
	var selectGrid;
	$(document).ready(function(){
		$("#main").ligerLayout({leftWidth:200,allowLeftCollapse:true,allowLeftResize:true});
		$("#toolBar").ligerToolBar({
			items:[]
			});
		$("#toolBar").append(
			' <div style="padding-top:5px;display:inline;"><label id="includeContainer"><input type="checkbox" onchange="searchData" name="includeChild" id="includeChild" checked="checked" />包含下级</label></div>'
			+' <div style="float:right;padding-right:50px;display:inline;">'
	    	+'	<form onsubmit="searchData();return false;">'	
		    +'		<input type="text" name="searchKeyWord" onfocus="if(this.value==this.defaultValue){this.value=\'\';}" onblur="if(this.value==\'\'){this.value=this.defaultValue;}" title="名称/编码" defaultValue="名称/编码" value="名称/编码" id="searchKeyWord" class="input"/>'
		    +'		<a class="mini-button" href="javascript:void(0)"><span class="mini-button-low-text" onclick="searchData();return false;">查询</span></a>'
	    	+'	</form>'
	    	+'</div>'
		);
		initDelayTree();
		$list_dataGrid = $("#tableContainer").ligerGrid($.extend($list_defaultGridParam,{
            columns: [ 
	            {display: '名称', name: 'name', align: 'left', width: 120},
	            {display: '编码', name: 'number', align: 'left', width: 120}
	        ],
	        delayLoad:true,
            url:getPath()+'/permission/permissionItem/listData'
            <c:if test="${multi != 'true'}">
				,onDblClickRow:function(){
		        	okSelect();
		        }
			</c:if>
			<c:if test="${multi == 'true'}">
				,checkbox:true
				,height:'300px'
			</c:if>
        }));
		
		if(multi==true){
			selectGrid = $('#selectTable').ligerGrid({
				allowUnSelectRow:true,
				width: '100%', 
			    height: '140px', 
			    rownumbers:true,
			    enabledSort:false,
			    usePager:false,
			    checkbox:true,
			    data:{Rows:[]},
			    columns: [ 
				            {display: '名称', name: 'name', align: 'left', width: 120},
				            {display: '编码', name: 'number', align: 'left', width: 120}
				        ],
			    onBeforeShowData:function(data){
			    	$list_currentPageData = data;
			    	//用于支持多级取值方式绑定如name:'data.aa.dd.bcc'的方式
			    	function updateColumnData(column){
			    		var name = column.columnname;
			    		if(name && name.indexOf('.')>0){
			    			var names = name.split('.');
			    			if(data.items){
				       			 for(var i = 0; i < data.items.length; i++){
				       				 var tmp = data.items[i];
				       				 for(var j = 0;j < names.length; j++){
				       					 if(tmp[names[j]]!=null){
				       						 tmp = tmp[names[j]];
				       					 }else{
				       						 tmp = null;
				       						 break;
				       					 }
				       				 }
				       				data.items[i][name] = tmp;
				       			 }
			    			}
			    		}
			    		if(column.columns && column.columns.length > 0){
			    			for(var i = 0; i < column.columns.length;i++){
			    				updateColumnData(column.columns[i]);
			    			}
			    		}
			    	}
			    	if(this.columns){
			    		for(var i = 0; i < this.columns.length;i++){
			    			updateColumnData(this.columns[i]);
			    		}
			    	}
			    	return true;
			    }
			});
			$('#toolBar2').ligerToolBar({
				items:[{id:'addToSelect',text:'选择',click:function(){
						var selectNodes = $list_dataGrid.getSelectedRows();
						if(selectNodes && selectNodes.length>0){
							for(var i = 0; i < selectNodes.length; i++){
								var n = selectNodes[i];
								selectNodes[i] = {};
								for(var t in n){
									selectNodes[i][t] = n[t];
								}
							}
							var selectedNodes = selectGrid.getData();
							if(selectedNodes==null || selectedNodes.length==0){
								selectGrid.addRows(selectNodes);
							}else{
								var flag = true;
								for(var i = 0; i < selectNodes.length; i++){
									flag = true;
									for(var j = 0; j < selectedNodes.length; j++){
										if(selectNodes[i].id==selectedNodes[i].id){
											flag = false;
											break;
										}
									}
									if(flag == true){
										selectGrid.addRow(selectNodes[i]);
									}
								}
							}
						}
					  },icon:'down_b'},
				      {id:'removeFromSelect',text:'移除',click:function(){
				    	  selectGrid.deleteSelectedRow();
				      },icon:'up_b'}]
			});
		}
		$('#includeContainer').bind('click',searchData);
	});
	function onTreeNodeClick(event, treeId, treeNode){
		$('#searchKeyWord').val($('#searchKeyWord').attr('defaultValue'));
		searchData();
	}
	function searchData(){
		var tree = $.fn.zTree.getZTreeObj("leftTree");
		var selectNodes = tree.getSelectedNodes();
		if(selectNodes.length>0){
			if($('#includeChild:checked').attr('checked') == true || $('#includeChild:checked').attr('checked') == 'checked'){
				delete $list_dataParam['group'];
				$list_dataParam['longNumber'] = selectNodes[0].longNumber;
			}else{
				$list_dataParam['group'] = selectNodes[0].id;
				delete $list_dataParam['longNumber'];
			}
		}else{
			delete $list_dataParam['group'];
			delete $list_dataParam['longNumber'];
		}
		var kw = $('#searchKeyWord').val();
		kw = kw.replace(/^\s+|\s+$/g,'');
		$('#searchKeyWord').val(kw);
		if(kw==$('#searchKeyWord').attr('defaultValue')){
			kw='';
		}
		if(kw==null || kw == ''){
			delete $list_dataParam['key'];
		}else{
			$list_dataParam['key'] = kw;
		}
		resetList();
	}
	function okSelect(){
		if(!multi){
			var selectRow = $list_dataGrid.getSelected();
			if(selectRow==null || selectRow.length==0){
				artDialog.alert('请选择数据');
			}else{
				artDialog.open.origin[art.dialog.data("returnFunName")].call(null, selectRow);
				art.dialog.close();
			}
		}else{
			var selectData = selectGrid.getData();
			if(selectData==null || selectData.length==0){
				artDialog.alert('请选择数据');
			}else{
				artDialog.open.origin[art.dialog.data("returnFunName")].call(null, selectData);
				art.dialog.close();
			}
		}
	}
</script>